﻿package Modules.MouseCursorModule
{
	import Modules.MainTimeLine;
	import Modules.Utilities;
	
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Point;

	public class MoveObjectTool extends Tool
	{
		//temp object is a guide for placement
		public var tempFarmObject:Bitmap;
		public var currentSelectedObject:MovieClip;
		public var intercept:Boolean;
		
		//Constructor
		public function MoveObjectTool(myObject:MovieClip, selectEvent:MouseEvent)
		{
			//call super
			super();
			
			//set intercept
			intercept = false;
			
			//Set the object to be moved
			currentSelectedObject = myObject;
			currentSelectedObject.mouseEnabled = false;
			currentSelectedObject.visible = false;
			
			if(currentSelectedObject.grid is biggriddata){
				tempFarmObject =  MainTimeLine.biggrid;
			}
			
			if(currentSelectedObject.grid is smallgriddata){
				tempFarmObject = MainTimeLine.smallgrid;
			}
			
			tempFarmObject.visible = true;
			toolMove(selectEvent);
		}
		
		override public function toolUp(event:MouseEvent):void{
			
			super.toolUp(event);
			
			if(click){
					
				if(!intercept){
					tempFarmObject.visible = false;
					currentSelectedObject.x = tempFarmObject.x;
					currentSelectedObject.y = tempFarmObject.y;
					currentSelectedObject.visible = true;
					currentSelectedObject.mouseEnabled = true;
					currentSelectedObject = null;
										
					//Update the object array in the farm itself
					MainTimeLine.farmcontainer.getFarmObjects();
					
					MainTimeLine.redecorateFunc(event);
					
				}
			}else{
				trace("exceed click time limit");
			}
		}
		
		override public function toolMove(event:MouseEvent):void{
			
			if(currentSelectedObject != null){
				tempFarmObject.x = event.localX-(tempFarmObject.width/2);
				tempFarmObject.y = event.localY-(tempFarmObject.height/2);
				
				if(!(event.target is Farm)){
					tempFarmObject.x += event.target.x;
					tempFarmObject.y += event.target.y;
				}
				
				tempFarmObject.x = Utilities.getNearestGridX(tempFarmObject.x);
				tempFarmObject.y = Utilities.getNearestGridY(tempFarmObject.y);
				
				intercept = !MainTimeLine.activeFarm.checkGridBounds(tempFarmObject.x, tempFarmObject.y);
				
				var i:int = 0;
				var currentObject:MovieClip;
				
				if(!intercept){
					for(i=0; i< MainTimeLine.numFarmObjects; i++){
						
						currentObject = MainTimeLine.farmObjects[i];
						
						if(currentSelectedObject != currentObject){
							if(currentObject.grid != null){
								if(currentObject.grid.hitTest(new Point(currentObject.x, currentObject.y),255,tempFarmObject,new Point(tempFarmObject.x, tempFarmObject.y),	255)){
									intercept = true;
									break;
								}else{
									intercept = false;
								}
							}else{
								intercept = false;
							}
						}
					}
				}
				
				if(intercept){
					tempFarmObject.bitmapData.applyFilter(tempFarmObject.bitmapData, tempFarmObject.bitmapData.rect, new Point(), new ColorMatrixFilter([1, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]));
				}else{
					tempFarmObject.bitmapData.applyFilter(tempFarmObject.bitmapData, tempFarmObject.bitmapData.rect, new Point(), new ColorMatrixFilter([0.75, 0.75, 0.75, 0, 0,0.75, 0.75, 0.75, 0, 0, 0.75, 0.75, 0.75, 0, 0, 0, 0, 0, 1, 0]));
				}
			}
		}
	}
}